package com.huqing.icu.config;

import com.huqing.icu.service.impl.MySQLChatMemory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description AI对话客户端
 * @Author huqing
 * @Date 2025/5/31 17:42
 **/
@Configuration
public class ChatClientConfig {

    /**
     * 定义会话存储方式为MySQL
     *
     * @return
     */
    /*@Bean
    public ChatMemory chatMemory() {
        //return new MySQLMemoryChatMemory();

        return MessageWindowChatMemory.builder()
                // 对话存储的repository存储库层的实现方式，如果不配置，默认也是 Spring 提供的 InMemoryChatMemoryRepository
                .chatMemoryRepository(new InMemoryChatMemoryRepository()) // 有默认
                .maxMessages(20) // 最大消息数
                .build();
    }*/
    @Bean
    public ChatMemory chatMemory() {
        return new MySQLChatMemory();
    }

    /**
     * @param model
     * @param chatMemory
     * @return
     */
    @Bean
    public ChatClient chatClient(OpenAiChatModel model, ChatMemory chatMemory) {
        return ChatClient
                .builder(model)
                //系统提示词
                .defaultSystem("你是一个智能助手，请回答用户的问题，要求回答问题尽量简洁，不要长篇大论")
                //SimpleLoggerAdvisor是用于记录对大模型的请求、响应的详细日志
                //MessageChatMemoryAdvisor：用于聊天记录持久化
                //Advisors是环绕增强，负责拦截请求，做日志记录、会话记录
                .defaultAdvisors(new SimpleLoggerAdvisor()
                        //将MySQLChatMemory加入环绕增强
                        //MessageChatMemoryAdvisor.builder(chatMemory).build()
                )

                //让大模型调用函数，输出函数返回的json对象，比如让大模型调用查询房源的函数，得到房源列表返回给前端，但是需要写好提示词
                //.defaultTools(new HouseTools())
                .build()
                ;
    }

   /* @Bean
    public VectorStore vectorStore(OpenAiEmbeddingModel model) {
        SimpleVectorStore simpleVectorStore = new SimpleVectorStore();
        return SimpleVectorStore;
    }*/


}
